import 'package:flutter/material.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import 'package:jiyun_behavior/screens/basic_chart_screen.dart';
import 'package:jiyun_behavior/screens/consultation_screen.dart';
import 'package:jiyun_behavior/screens/guidance_screen.dart';
import 'package:jiyun_behavior/screens/membership_screen.dart';
import 'package:jiyun_behavior/screens/profile_screen.dart';
import 'package:jiyun_behavior/screens/scripture_screen.dart';
import 'package:jiyun_behavior/utils/app_theme.dart';

class HomeScreen extends StatefulWidget {
  const HomeScreen({Key? key}) : super(key: key);

  @override
  State<HomeScreen> createState() => _HomeScreenState();
}

class _HomeScreenState extends State<HomeScreen> {
  int _currentIndex = 0;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: IndexedStack(
        index: _currentIndex,
        children: [
          _buildHomeTab(),
          const BasicChartScreen(),
          const ScriptureScreen(),
          const GuidanceScreen(),
          const ProfileScreen(),
        ],
      ),
      bottomNavigationBar: BottomNavigationBar(
        currentIndex: _currentIndex,
        type: BottomNavigationBarType.fixed,
        selectedItemColor: AppTheme.primaryColor,
        unselectedItemColor: AppTheme.lightTextColor,
        onTap: (index) {
          setState(() {
            _currentIndex = index;
          });
        },
        items: const [
          BottomNavigationBarItem(
            icon: Icon(Icons.home),
            label: '首页',
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.pie_chart),
            label: '命盘',
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.book),
            label: '抄经',
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.compass_calibration),
            label: '指引',
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.person),
            label: '我的',
          ),
        ],
      ),
    );
  }

  Widget _buildHomeTab() {
    return Scaffold(
      appBar: AppBar(
        title: const Text('吉运行为'),
      ),
      body: SafeArea(
        child: SingleChildScrollView(
          child: Padding(
            padding: const EdgeInsets.all(16.0),
            child: Column(
              children: [
                _buildWelcomeCard(),
                const SizedBox(height: 20),
                _buildFeatureGrid(),
                const SizedBox(height: 20),
                _buildDailyFortune(),
                const SizedBox(height: 20),
                _buildRecommendedScriptures(),
              ],
            ),
          ),
        ),
      ),
    );
  }

  Widget _buildWelcomeCard() {
    return Card(
      child: Padding(
        padding: const EdgeInsets.all(20.0),
        child: Column(
          children: [
            Row(
              children: [
                Container(
                  width: 60,
                  height: 60,
                  decoration: BoxDecoration(
                    color: AppTheme.secondaryColor,
                    shape: BoxShape.circle,
                  ),
                  child: const Center(
                    child: Icon(
                      Icons.person,
                      size: 30,
                      color: AppTheme.primaryColor,
                    ),
                  ),
                ),
                const SizedBox(width: 16),
                Expanded(
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      const Text(
                        '张先生，下午好！',
                        style: TextStyle(
                          fontSize: 18,
                          fontWeight: FontWeight.bold,
                        ),
                      ),
                      const SizedBox(height: 4),
                      Text(
                        '今日宜静心修行，提升内在能量',
                        style: TextStyle(
                          fontSize: 14,
                          color: AppTheme.subtitleColor,
                        ),
                      ),
                    ],
                  ),
                ),
              ],
            ),
            const SizedBox(height: 20),
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceAround,
              children: [
                _buildStatItem('128', '福报值'),
                _buildStatItem('7', '连续抄经'),
                _buildStatItem('3', '剩余咨询'),
              ],
            ),
          ],
        ),
      ),
    );
  }

  Widget _buildStatItem(String value, String label) {
    return Column(
      children: [
        Text(
          value,
          style: const TextStyle(
            fontSize: 24,
            fontWeight: FontWeight.bold,
            color: AppTheme.primaryColor,
          ),
        ),
        const SizedBox(height: 4),
        Text(
          label,
          style: TextStyle(
            fontSize: 12,
            color: AppTheme.subtitleColor,
          ),
        ),
      ],
    );
  }

  Widget _buildFeatureGrid() {
    return GridView.count(
      crossAxisCount: 4,
      shrinkWrap: true,
      physics: const NeverScrollableScrollPhysics(),
      children: [
        _buildFeatureItem(
          icon: Icons.pie_chart,
          label: '命盘',
          onTap: () {
            setState(() {
              _currentIndex = 1;
            });
          },
        ),
        _buildFeatureItem(
          icon: Icons.book,
          label: '抄经',
          onTap: () {
            setState(() {
              _currentIndex = 2;
            });
          },
        ),
        _buildFeatureItem(
          icon: Icons.compass_calibration,
          label: '指引',
          onTap: () {
            setState(() {
              _currentIndex = 3;
            });
          },
        ),
        _buildFeatureItem(
          icon: Icons.question_answer,
          label: '咨询',
          onTap: () {
            Navigator.push(
              context,
              MaterialPageRoute(builder: (context) => const ConsultationScreen()),
            );
          },
        ),
        _buildFeatureItem(
          icon: Icons.card_membership,
          label: '会员',
          onTap: () {
            Navigator.push(
              context,
              MaterialPageRoute(builder: (context) => const MembershipScreen()),
            );
          },
        ),
        _buildFeatureItem(
          icon: Icons.history,
          label: '记录',
          onTap: () {
            // 查看记录逻辑
          },
        ),
        _buildFeatureItem(
          icon: Icons.star,
          label: '收藏',
          onTap: () {
            // 查看收藏逻辑
          },
        ),
        _buildFeatureItem(
          icon: Icons.more_horiz,
          label: '更多',
          onTap: () {
            // 更多功能逻辑
          },
        ),
      ],
    );
  }

  Widget _buildFeatureItem({
    required IconData icon,
    required String label,
    required VoidCallback onTap,
  }) {
    return GestureDetector(
      onTap: onTap,
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Container(
            width: 50,
            height: 50,
            decoration: BoxDecoration(
              color: AppTheme.secondaryColor,
              borderRadius: BorderRadius.circular(12),
            ),
            child: Center(
              child: Icon(
                icon,
                color: AppTheme.primaryColor,
              ),
            ),
          ),
          const SizedBox(height: 8),
          Text(
            label,
            style: const TextStyle(
              fontSize: 12,
            ),
          ),
        ],
      ),
    );
  }

  Widget _buildDailyFortune() {
    return Card(
      child: Padding(
        padding: const EdgeInsets.all(16.0),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Text(
                  '今日运势',
                  style: Theme.of(context).textTheme.titleLarge,
                ),
                Text(
                  '2023年6月11日',
                  style: TextStyle(
                    fontSize: 14,
                    color: AppTheme.subtitleColor,
                  ),
                ),
              ],
            ),
            const SizedBox(height: 16),
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceAround,
              children: [
                _buildFortuneItem('总运', '吉'),
                _buildFortuneItem('财运', '中'),
                _buildFortuneItem('事业', '吉'),
                _buildFortuneItem('健康', '吉'),
              ],
            ),
            const SizedBox(height: 16),
            Text(
              '今日五行缺火，宜穿红色衣物，饮食宜温补，忌生冷食物。事业上有贵人相助，可大胆推进计划。',
              style: TextStyle(
                fontSize: 14,
                color: AppTheme.textColor,
                height: 1.6,
              ),
            ),
            const SizedBox(height: 16),
            OutlinedButton(
              onPressed: () {
                setState(() {
                  _currentIndex = 3; // 切换到指引页面
                });
              },
              style: OutlinedButton.styleFrom(
                minimumSize: const Size(double.infinity, 40),
              ),
              child: const Text('查看详细运势分析'),
            ),
          ],
        ),
      ),
    );
  }

  Widget _buildFortuneItem(String label, String value) {
    Color valueColor;
    if (value == '吉') {
      valueColor = const Color(0xFF52C41A);
    } else if (value == '凶') {
      valueColor = const Color(0xFFF5222D);
    } else {
      valueColor = const Color(0xFFFA8C16);
    }

    return Column(
      children: [
        Text(
          value,
          style: TextStyle(
            fontSize: 20,
            fontWeight: FontWeight.bold,
            color: valueColor,
          ),
        ),
        const SizedBox(height: 4),
        Text(
          label,
          style: TextStyle(
            fontSize: 12,
            color: AppTheme.subtitleColor,
          ),
        ),
      ],
    );
  }

  Widget _buildRecommendedScriptures() {
    return Card(
      child: Padding(
        padding: const EdgeInsets.all(16.0),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Text(
              '今日推荐经书',
              style: Theme.of(context).textTheme.titleLarge,
            ),
            const SizedBox(height: 16),
            SizedBox(
              height: 200,
              child: ListView(
                scrollDirection: Axis.horizontal,
                children: [
                  _buildScriptureItem('太上感应篇', AppTheme.secondaryColor, AppTheme.primaryColor, '提升福报'),
                  const SizedBox(width: 12),
                  _buildScriptureItem('心经', const Color(0xFFE6F7FF), const Color(0xFF1890FF), '化解烦恼'),
                  const SizedBox(width: 12),
                  _buildScriptureItem('道德经', const Color(0xFFF6FFED), const Color(0xFF52C41A), '增长智慧'),
                ],
              ),
            ),
            const SizedBox(height: 16),
            OutlinedButton(
              onPressed: () {
                setState(() {
                  _currentIndex = 2; // 切换到抄经页面
                });
              },
              style: OutlinedButton.styleFrom(
                minimumSize: const Size(double.infinity, 40),
              ),
              child: const Text('开始今日抄经'),
            ),
          ],
        ),
      ),
    );
  }

  Widget _buildScriptureItem(String title, Color bgColor, Color iconColor, String description) {
    return GestureDetector(
      onTap: () {
        setState(() {
          _currentIndex = 2; // 切换到抄经页面
        });
      },
      child: Column(
        children: [
          Container(
            width: 120,
            height: 160,
            decoration: BoxDecoration(
              color: bgColor,
              borderRadius: BorderRadius.circular(8),
            ),
            child: Center(
              child: Icon(
                FontAwesomeIcons.bookOpen,
                size: 36,
                color: iconColor,
              ),
            ),
          ),
          const SizedBox(height: 8),
          Text(
            title,
            style: const TextStyle(
              fontSize: 14,
              fontWeight: FontWeight.bold,
            ),
          ),
          Text(
            description,
            style: TextStyle(
              fontSize: 12,
              color: AppTheme.subtitleColor,
            ),
          ),
        ],
      ),
    );
  }
}
 